<template>
  <div class="zenbu-list">
    <div class="page-header">
      <div class="header-actions">
        <el-button @click="$router.push('/main/biao/zenbu')" icon="el-icon-arrow-left">返回</el-button>
      </div>
      <h1>人员增补申请单列表</h1>
    </div>

    <div class="main-content">
      <div class="search-section">
        <el-form :inline="true" :model="queryParams" class="search-form">
          <el-form-item>
            <el-date-picker v-model="queryParams.fillDate" type="date" value-format="yyyy-MM-dd" placeholder="选择填单日期"
              clearable>
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-select v-model="queryParams.applicationDepartment" placeholder="选择申请部门" clearable>
              <el-option label="生产部" value="生产部"></el-option>
              <el-option label="资材部" value="资材部"></el-option>
              <el-option label="品质部" value="品质部"></el-option>
              <el-option label="财务部" value="财务部"></el-option>
              <el-option label="销售部" value="销售部"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryParams.applicant" placeholder="输入申请人姓名" clearable>
              <i slot="prefix" class="el-icon-search"></i>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-select v-model="queryParams.employeeType" placeholder="选择员工类别" clearable>
              <el-option label="直接员工" value="直接员工"></el-option>
              <el-option label="间接员工" value="间接员工"></el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="$router.push('/main/biao/zenbu')">
            新增申请
          </el-button>
          <el-form-item class="search-buttons">
          </el-form-item>
        </el-form>
      </div>

      <div class="table-section">
        <div class="table-header">
          <!-- <div class="table-title">申请单列表</div> -->
        </div>

        <el-table :data="tableData" border v-loading="loading" class="main-table">
          <el-table-column prop="fillDate" label="填单日期" min-width="120">
          </el-table-column>
          <el-table-column prop="applicationDepartment" label="申请部门" min-width="120">
          </el-table-column>
          <el-table-column prop="applicationPosition" label="申请职位" min-width="120">
          </el-table-column>
          <el-table-column prop="applicant" label="申请人" min-width="120">
          </el-table-column>
          <el-table-column prop="employeeType" label="员工类别" min-width="120">
          </el-table-column>
          <el-table-column prop="demandDate" label="需求日期" min-width="120">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="300" align="center">
            <template slot-scope="scope">
              <el-button @click="handleView(scope.row)" type="text" size="small" icon="el-icon-view">
                查看
              </el-button>
              <el-button @click="handleEdit(scope.row)" type="text" size="small" icon="el-icon-edit">
                编辑
              </el-button>
              <el-button @click="handleDelete(scope.row)" type="text" size="small" icon="el-icon-delete"
                class="delete-button">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination-container">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="currentPage" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import zenbuApi from '@/api/zenbu';

export default {
  name: 'ZenbuList',
  data() {
    return {
      // 添加查询参数对象
      queryParams: {
        fillDate: '',
        applicationDepartment: '',
        applicant: '',
        employeeType: ''
      },
      tableData: [],
      loading: false,
      currentPage: 1,
      pageSize: 10,
      total: 0
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    // 修改 fetchData 方法，添加查询参数
    async fetchData() {
      try {
        this.loading = true;
        const params = {
          ...this.queryParams,
          current: this.currentPage,
          size: this.pageSize
        };

        const response = await zenbuApi.getList(params);
        this.tableData = response.records;
        this.total = response.total;
      } catch (error) {
        this.$message.error('获取列表失败：' + error.message);
      } finally {
        this.loading = false;
      }
    },

    // 添加查询方法
    handleQuery() {
      this.currentPage = 1;
      this.fetchData();
    },

    // 添加重置方法
    resetQuery() {
      this.queryParams = {
        fillDate: '',
        applicationDepartment: '',
        applicant: '',
        employeeType: ''
      };
      this.currentPage = 1;
      this.fetchData();
    },

    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    handleView(row) {
      this.$router.push(`/main/biao/zenbu?id=${row.id}&mode=view`);
    },
    handleEdit(row) {
      this.$router.push(`/main/biao/zenbu?id=${row.id}&mode=edit`);
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该记录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });

        await zenbuApi.delete(row.id);
        this.$message.success('删除成功');
        this.fetchData();
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('删除失败：' + error.message);
        }
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.zenbu-list {
  padding: 12px;
  // background-color: #f5f7fa;
  min-height: 100vh;
  width: 1200px;
  margin: 0 auto;
}

.page-header {
  margin-bottom: 12px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  padding: 8px 12px;
  position: relative;

  .header-actions {
    position: absolute;
    left: 12px;
    top: 20px;
  }

  h1 {
    text-align: center;
    color: #2c3e50;
    font-size: 18px;
    margin: 0;
    font-weight: 500;
  }
}

.main-content {
  .search-section {
    background: #fff;
    border-radius: 4px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
    margin-bottom: 12px;
  }

  .search-form {
    padding: 12px;
    display: flex;
    justify-content: center;
    gap: 8px;
    align-items: center;

    .el-form-item {
      margin: 0;
    }

    .search-buttons {
      margin-left: auto;
    }

    :deep(.el-input__inner) {
      height: 32px;
      line-height: 32px;
      transition: all 0.3s;

      &:hover {
        border-color: #409EFF;
      }

      &:focus {
        border-color: #409EFF;
        box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
      }
    }

    :deep(.el-range-editor.el-input__inner) {
      height: 32px;
      padding: 0 8px;
    }

    :deep(.el-button) {
      height: 32px;
      padding: 0 12px;
    }
  }
}

.table-section {
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  padding: 12px;

  .table-header {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 12px;

    .table-title {
      font-size: 15px;
      font-weight: 500;
      color: #2c3e50;
    }

    .el-button {
      padding: 6px 12px;
      height: 32px;
    }
  }

  :deep(.el-table th.el-table__cell.is-leaf) {
    background-color: #f5f7fa;
  }

  .main-table {
    margin-bottom: 12px;

    :deep(.el-table__header-wrapper) {
      th {
        background-color: #f5f7fa;
        color: #606266;
        font-weight: 500;
        // padding: 8px 0;
        height: 40px;
      }
    }

    :deep(.el-table__body-wrapper) {
      td {
        padding: 6px 0;
        height: 40px;
      }
    }

    :deep(.el-button--text) {
      padding: 0 6px;
      height: 28px;
      line-height: 28px;

      &.delete-button {
        color: #F56C6C;
      }
    }
  }
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  padding-top: 12px;
  border-top: 1px solid #ebeef5;
  justify-content: center;

  :deep(.el-pagination) {
    button {
      height: 28px;
      min-width: 28px;
    }

    .el-pagination__sizes {
      height: 28px;
    }

    .el-pager li {
      height: 28px;
      min-width: 28px;
      line-height: 28px;
    }
  }
}

@media screen and (max-width: 768px) {
  .zenbu-list {
    padding: 8px;
  }

  .page-header {
    padding: 8px;

    .header-actions {
      position: static;
      margin-bottom: 8px;
    }

    h1 {
      font-size: 16px;
    }
  }

  .search-form {
    flex-direction: column;
    gap: 6px;

    .el-form-item {
      width: 100%;

      .el-input,
      .el-select,
      .el-date-picker {
        width: 100%;
      }
    }

    .search-buttons {
      width: 100%;
      display: flex;
      gap: 6px;

      .el-button {
        flex: 1;
      }
    }
  }

  .table-section {
    padding: 8px;

    .table-header {
      flex-direction: column;
      gap: 8px;

      .el-button {
        width: 100%;
      }
    }
  }

  .pagination-container {
    :deep(.el-pagination) {
      width: 100%;
      justify-content: center;

      .el-pagination__sizes {
        display: none;
      }
    }
  }
}
</style>